package com.write.program.db.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2020-05-03
 * Time: 18:10
 */
public class DaoUtil {

    private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_IP = "localhost";
    private static final int DB_PORT = 3306;
    private static final String DB_NAME = "Travel";
    private  static final String DB_USERNAME = "root";
    private  static final String DB_PASSWORD = "123456";
    private static final String DB_URL = "jdbc:mysql://" + DB_IP + ":" + DB_PORT + "/" + DB_NAME + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";


    private static Connection connection;

    public static Connection getConnect() throws SQLException, ClassNotFoundException {
        if (connection == null) {
            Class.forName(DB_DRIVER);
            connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
        }
        return connection;
    }

    public static void close(Connection connection) throws SQLException {
        connection.close();
    }

}
